class GameOverScene extends BaseScene {
	
	/** 返回按钮*/
	private titleLbl:eui.Label;
	/**已超越xxx */
	private titleLbl1:eui.Label;
	/**包含排行榜的容器 */
	private boxGroup:eui.Group;
	/** 分数*/
	private scoreTxt: eui.Label;
	/** 层*/
	private scoreTxt1: eui.Label;


	/** 第三方小程序小游戏图片*/
	private jumpIcon: eui.Image;

	/**发起挑战*/ 
	private shareImg: eui.Image;

	/**返回首页 */
	private goHomeImg:eui.Image;
	/**再来一局*/ 
	private playAgainImg: eui.Image;

	private goAllFriendRankRect:eui.Rect;
	private adList:Array<any>=[];
	private currentAdIndex:number = 0;
	private socreCrossList = [
		{minScore:1,maxScore:10,minCross:1,maxCross:5},
		{minScore:10,maxScore:50,minCross:5,maxCross:10},
		{minScore:50,maxScore:100,minCross:10,maxCross:20},
		{minScore:100,maxScore:200,minCross:20,maxCross:30},
		{minScore:200,maxScore:500,minCross:30,maxCross:50},
		{minScore:500,maxScore:800,minCross:50,maxCross:70},
		{minScore:800,maxScore:1200,minCross:70,maxCross:75},
		{minScore:1200,maxScore:1500,minCross:75,maxCross:80},
		{minScore:1500,maxScore:2000,minCross:80,maxCross:85},
		{minScore:2000,maxScore:3000,minCross:85,maxCross:90},
		{minScore:3000,maxScore:5000,minCross:90,maxCross:99}
		];


	bitmapdata: egret.BitmapData;
	bitmap: egret.Bitmap;
	public constructor(index: number = GameConfig.uiIndex.over) {
		super(index);
		this.skinName = "GameOverSceneSkin";

		// 是否展示广告,右上角左右旋转
		GameConfig.requestAdList(()=>{
			GameConfig.changeCurrentAd(()=>{
				this.updateAdView();
			});
		});
	}
	public viewWillAppear(){
        super.viewWillAppear();
        GameConfig.showBannerAd();
    }
    public viewWillDisAppear(){
        super.viewWillDisAppear();
        GameConfig.hideBannerAd();
    }
	public onChildrenCreated() {
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, GameConfig.uiIndex.recommend);
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, GameConfig.uiIndex.gameReborn);
		this.initView();
	}
	private async initView() {

		if(UserData.score_isWIN){
			this.titleLbl.text = "破纪录！";
			this.shareImg.source = "showScore_png";
		}else{
			this.titleLbl.text = "本次战绩";
			this.shareImg.source = "fight_png";
		}
		let cross = this.calcuteScoreCross(UserData.score);
		this.titleLbl1.text = "已超越全国"+Math.floor(cross)+"%的玩家";

		//刷新纪录时分数使用彩色字体
		this.scoreTxt.text = UserData.score+"";
		let scoreWidth = this.scoreTxt.width;
		this.scoreTxt.x = (this.scoreTxt.parent.width-scoreWidth)/2;
		this.scoreTxt1.x = this.scoreTxt.x+this.scoreTxt.width+10;
		
		this.requestUserInfo();

	}
	private calcuteScoreCross(score:number): number{
		if(score <= 0){
			return 0;
		}else if(score >= 5000){
			return 99;
		}else{
			for(let i=0;i<this.socreCrossList.length;i++){
				let data = this.socreCrossList[i];
				if(data.minScore <= score && score < data.maxScore){
					let cross = data.minCross;
					cross = cross+(data.maxCross-1-data.minCross)/(data.maxScore-1-data.minScore)*(score-data.minScore);
					return Math.floor(cross);
				}
			}
		}
		
	}
	private requestUserInfo(){
		 App.HttpRequestManager.request(NetConstants.GET_INFO, "", this.responseUserInfo.bind(this),egret.HttpMethod.GET);
	}
	private responseUserInfo(res){
		if (res.statusCode == 200 && res.data.code == 0) {
            UserData.saveUserInfo(res.data.data);
        } else {
            
        }
	}
	/**
	 * 发起挑战
	 */
	private shareGame(evt:TouchEvent){
		if (evt.type == egret.TouchEvent.TOUCH_END) {
			// 发起挑战 策略B
			let query = {from:GameConfig.shareFrom.overPageChallenge2Share};
			let shareData = GameConfig.getShareData(GameConfig.shareInfoB2,query);
			if(UserData.score_isWIN){
				// 炫耀战绩 策略B
				shareData = GameConfig.getShareData(GameConfig.shareInfoB1,query,UserData.score);
			}
			platform.shareAppMessage(shareData);
		}
	}
	private shareSuccess(data) {
		/**新版本微信不回调是否分享成功 */
	}
	/**
	 * 点击在来一局
	 */
	private tryAgain(evt:TouchEvent){
		if (evt.type == egret.TouchEvent.TOUCH_END) {
			App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, GameConfig.uiIndex.game);
			App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, this.index);
			App.EventManager.dispatchEvent(GameConstants.UI_ADD_TO_PARENT,GameConfig.uiIndex.game);
		}
	}
	/**
	 * 查看全部排行榜
	 */
	private showAllRank() {
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, this.index);
		App.EventManager.dispatchEvent(GameConstants.UI_ADD_TO_PARENT,GameConfig.uiIndex.rankListFromOver);
	}
	/**
	 * 点击返回页面
	 */
	private goHome() {
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, GameConfig.uiIndex.game);
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, this.index);
		App.EventManager.dispatchEvent(GameConstants.UI_ADD_TO_PARENT,GameConfig.uiIndex.home);
	}
	private updateAdView(){
		if(!UserData.is_verify && GameConfig.currentAd){
			
			egret.Tween.removeTweens(this.jumpIcon);
			this.jumpIcon.rotation = 0;
			let ad = GameConfig.currentAd;
			this.jumpIcon.visible = true;
			this.jumpIcon.source = ad.icon;
			egret.Tween.removeTweens(this.jumpIcon);
			egret.Tween.get(this.jumpIcon, { loop: true }).wait(1200).to({ rotation: 8 }, 100).to({ rotation: -8 }, 200).to({ rotation: 0 }, 100);
		}else{
			this.jumpIcon.visible = false;
		}
	}
	private jumpAd(){
		if(!GameConfig.currentAd){
			return;
		}
		
		egret.setTimeout(()=>{
			GameConfig.changeCurrentAd(()=>{
				this.updateAdView();
			});
		},this,2000);
		let ad = GameConfig.currentAd;
		this.requestAdCpc(ad);
		if(ad.appid){
			let param = {appId:ad.appid};
			if(ad.path){
				param["path"] = ad.path;
			}
			if(ad.envVersion){
				param["envVersion"] = ad.envVersion;
			}
			if(ad.extraData){
				param["extraData"] = ad.extraData;
			}
			param["complete"] = this.jumpAdComplete.bind(this);
			
			platform.callPlatformMethod("navigateToMiniProgram",param);
		}else{
			let image = ad.previewImage;
			if(!image){
				image = ad.image;
			}
			platform.previewImage({ urls: [image]});
		}
	}
	private jumpAdComplete(res){
		if(Utils.callWXSuccess(res)){
		}else{
			let ad = GameConfig.currentAd;
			let image = ad.previewImage;
			if(!image){
				image = ad.image;
			}
			platform.previewImage({ urls: [image]});
		}
	}
	private requestAdCpc(ad:any){
		let param = {
						user_id:UserData.id,
						ad_appid:ad.appid,
						on_appid:GameConfig.appid
					};
		App.HttpRequestManager.request(NetConstants.AD_CPC, param, (res)=>{
			console.log("requestAdCpc-",res);
		});
	}
	public addListener() {
		super.addListener();

		// 展示推荐游戏
		this.jumpIcon.addEventListener(egret.TouchEvent.TOUCH_TAP, this.jumpAd, this);
		// 查看全部排行榜
		this.goAllFriendRankRect.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showAllRank, this);
		// 发起挑战
		ViewUtils.addTapEvent(this.shareImg,this.shareGame,this,{scaleX:0.9,scaleY:0.9});
		// 再玩一局
		ViewUtils.addTapEvent(this.playAgainImg,this.tryAgain,this,{scaleX:0.9,scaleY:0.9});

		// 返回首页
		this.goHomeImg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.goHome, this);
		


	}
	public removeListener() {
		super.removeListener();
		
		// 展示推荐游戏
		this.jumpIcon.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.jumpAd, this);
		// 查看全部排行榜
		this.goAllFriendRankRect.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.showAllRank, this);
		// 发起挑战
		ViewUtils.removeTapEvent(this.shareImg,this);
		// 再玩一局
		ViewUtils.removeTapEvent(this.playAgainImg,this);
		// 返回首页
		this.goHomeImg.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.goHome, this);
	}
}